﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using The_Last_Knights.GUI;

namespace The_Last_Knights.Helper.Xml
{
    public class BorderReader: ReaderEntity
    {                
        public BorderReader()
        {
            
        }

        public override object Read(XElement element, string ID, int level)
        {                        
            string direction = element.Attribute(XName.Get(@"Direction")).Value;
            var defs = element.Descendants(XName.Get(@"Element"));
            Dictionary<string, object> dictionary = new Dictionary<string, object>();            
            Vector2Reader vector2Reader = new Vector2Reader();
            Vector2[] vector2s = new Vector2[2];
            int index = 0;
            foreach (var xElement in defs)
            {
                string name = xElement.Attribute(XName.Get(@"ID")).Value;                

                string type = xElement.Attribute(XName.Get(@"Type")).Value;
                int lev = int.Parse(xElement.Attribute(XName.Get(@"Level")).Value);
                if (lev == level + 1)
                    switch (type)
                    {
                        case "Vector2":
                            vector2s[index] = (Vector2) Factory.Read(xElement, name, type, lev);
                            index++;
                            break;
                        default:
                            dictionary.Add(name, Factory.Read(xElement, null, type, lev));
                            break;                           
                    }                    
            }
            Border border = new Border(Game, ID, vector2s[0], vector2s[1], null, direction);
            GameEntityDictionary.Add(dictionary);
            return border;
        }
    }
}
